package id.co.shuba.spamobile.list;

import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.SimpleCursorLoader;
import id.co.shuba.spamobile.data.DSARDB;
import id.co.shuba.spamobile.data.ISharedDB;
import id.co.shuba.spamobile.widget.DSARCursorAdapter;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class DSARListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {
	
	public interface OnDSARSelectedListener {
        public void onDSARSelected(int id, boolean lastItem);
    }		
	
	private OnDSARSelectedListener _listener;	
	
	private DSARDB _db;
	
	private DSARCursorAdapter _adapter;
	private String _prospectID;
	
	public void setOnDSARSelectedListener(OnDSARSelectedListener listener) {
		_listener = listener;
	}

	public static final class DSARCursorLoader extends SimpleCursorLoader {		
		
		private final DSARDB _db;		
		private final String _prospectID;

		public DSARCursorLoader(Context context, DSARDB db, String prospectID) {
			super(context);
			_db = db;
			_prospectID = prospectID;
		}			

		@Override
		public Cursor loadInBackground() {						
			return _db.getList(_prospectID);
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (_listener == null)
			return;
						
		_listener.onDSARSelected((int)id, position == l.getAdapter().getCount() - 1);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
							
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		_db = new DSARDB(((ISharedDB)getActivity()).getDB());
		
		Bundle bundle = new Bundle();
	    bundle.putString("prospectID", _prospectID);
        getLoaderManager().initLoader(0, bundle, this);
	}

	public void setProspectID(String prospectID) {
		_prospectID = prospectID;		
	}	
	
	public void reload() {
		Bundle bundle = new Bundle();
	    bundle.putString("prospectID", _prospectID);

	    getLoaderManager().restartLoader(0, bundle, this);
	}
	
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String prospectID = null;
		if (args != null)
			prospectID = args.getString("prospectID");				
		
		return new DSARCursorLoader(getActivity(), _db, prospectID);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		_adapter.swapCursor(cursor);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		_adapter.swapCursor(null);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.dsar_list_fragment, container, false);
		
		_adapter = new DSARCursorAdapter(getActivity(), null, true);		
		setListAdapter(_adapter);
		
		return v;
	}
}
